(define-module (bit4bit crystal)
  #:use-module (gnu packages llvm)
  #:use-module (gnu packages tls)
  #:use-module (gnu packages libevent)
  #:use-module (gnu packages base)
  #:use-module (gnu packages pcre)
  #:use-module (gnu packages commencement)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system copy)
  )

(define-public crystal-binary
  (package
    (name "crystal-binary")
    (version "1.2.2")
    (source
     (origin
       (method url-fetch)
       (uri (string-append
	     "https://github.com/crystal-lang/crystal/releases/download/"
	     version
	     "/crystal-"
	     version
	     "-1-linux-x86_64.tar.gz"))
       (sha256 (base32 "18dk38h41q2nqphyi1jh5ga87kas9prdwqnymgja1zsn5236fvmi"))))
    (build-system copy-build-system)
    (inputs
     `(("gcc-toolchain" ,gcc-toolchain)
       ("pcre" ,pcre)
       ("libevent" ,libevent)))

    (propagated-inputs
     `(("gcc-toolchain:static" ,gcc-toolchain "static")
       ("pcre:static" ,pcre "static")))

    (arguments
     `(#:phases
       (modify-phases %standard-phases
	 (add-after 'install 'wrap-binary
	   (lambda* (#:key inputs outputs #:allow-other-keys)
		    (let* ((out (assoc-ref outputs "out"))
			   (crystal-bin (string-append out "/bin/crystal"))
			   (pcre-path (string-append
				       (assoc-ref inputs "pcre")
				       "/lib"))
			   (libevent-path (string-append
					   (assoc-ref inputs "libevent")
					   "/lib")))

	       (wrap-program crystal-bin
			     `("CRYSTAL_OPTS" ":" = (,(string-append "--link-flags -L" libevent-path " --link-flags -L" pcre-path)))
			     '("CC" ":" = ("gcc")))
	     #t))))))
    (synopsis "Crystal is a general-purpose, object-oriented programming language.")
    (description "Crystal is a general-purpose, object-oriented programming language.")
    (home-page "https://www.crystal-lang.org")
    (license license:asl2.0)
    ))
